package emap;

import java.io.*;
import java.util.*;

class Map 
{
	private int myID;
	private String myName;
	private String myTilesetName;
	private int myYLength, myXLength;
	private int myLevels; //Goku.myLevels>9000
	
	private GridSquare[][] myGrid;
	
	public Map(String filename)
	{
		load(filename);
	}
	private boolean load(String mapName) //returns true if the map loaded correctly.
	{
		String filename = "emap/maps/" + mapName + ".txt";
		try
		{
			Scanner infile = new Scanner(new FileReader(filename));
			String str = "";
			String check = "#enatoma_map";
			str = infile.next();
			if (!(str.length() >= check.length() && str.substring(0, check.length()).equals(check)))
			{
				infile.close();
				return false;
			}
			
			
			//Read in map ID#
			check = "#map_id:";
			str = infile.next();
			if (str.length() >= check.length() && str.substring(0, check.length()).equals(check))
			{
				myID = infile.nextInt();
			}
			else
			{
				infile.close();
				return false;
			}
			System.out.println("ID: " + myID);
			
			
			//Read in map name
			check = "#name:";
			str = infile.next();
			if (str.length() >= check.length() && str.substring(0, check.length()).equals(check))
			{
				myName = infile.next();
				System.out.println("Map name: " + myName);
			}
			else
			{
				System.out.println("woo! 0_0 :" + str);
				infile.close();
				return false;
			}
			
			
			//Read in tileset name
			check = "#tileset:";
			str = infile.next();
			if (str.length() >= check.length() && str.substring(0, check.length()).equals(check))
			{
				myTilesetName = infile.next();
				System.out.println("Tileset: " + myName);
			}
			else
			{
				infile.close();
				return false;
			}
				
			//Read in map dimensions
			int levels;
			check = "#dimensions:";
			str = infile.next();
			if (str.length() >= check.length() && str.substring(0, check.length()).equals(check))
			{
				myXLength = infile.nextInt();
				myYLength = infile.nextInt();
				levels = infile.nextInt();
				System.out.println("Map size: " + myXLength + " by " + myYLength + " with " + levels + " levels.");
			}
			else
			{
				infile.close();
				return false;
			}
				
			//Read in map level values and construct map.
			myGrid = new GridSquare[myXLength][myYLength];
			
			//read data into 2 temporary arrays
			
			int[][][] tileGrid = new int[myXLength][myYLength][levels];
			int[][][] levelGrid = new int[myXLength][myYLength][levels];
			for (int lev = 0; lev < levels; lev++)
			{
				
				for (int y = 0; y < myYLength; y++)
				{
					for (int x = 0; x < myXLength; x++)
					{
							tileGrid[x][y][lev] = infile.nextInt();
					}
				}
	
				for (int y = 0; y < myYLength; y++)
				{
					for (int x = 0; x < myXLength; x++)
					{
						levelGrid[x][y][lev] = infile.nextInt();
					}
				}
			}
			/*System.out.println("Map: " + myName);
			for (int lev = 0; lev < levels; lev++)
			{
				for (int y = 0; y < myYLength; y++)
				{
					for (int x = 0; x < myXLength; x++)
					{
						System.out.print(tileGrid[x][y][lev] + " ");
					}
					System.out.println("");
				}
				System.out.println("");
			}*/
			
			//****************************************************************\\
			myLevels=levels;
			myGrid = new GridSquare[myXLength][myYLength];
			for (int y = 0; y < myYLength; y++)
			{
				for (int x = 0; x < myXLength; x++)
				{
					(myGrid[x][y])=new GridSquare();
					for (int lev = 0; lev < levels; lev++)
					{
						(myGrid[x][y]).addTile(new Tile(tileGrid[x][y][lev],levelGrid[x][y][lev]));//tileGrid[x][y][lev], levelGrid[x][y][lev]
					}
					(myGrid[x][y]).sortThineSelf();
				}
			}
			
		}
		catch(FileNotFoundException fnfeRef)
		{
			System.out.println(fnfeRef.toString());
			return false;
		}
		return false;
	}
	
	
	
	public int getYLength()
	{
		return myYLength;
	}
	public int getXLength()
	{
		return myXLength;
	}
	public int getLevels()
	{
		return myLevels;
	}
	public String getTilesetName()
	{
		return myTilesetName;
	}
	public GridSquare getSquare(int sqxpos,int sqypos)
	{
		if(sqxpos<0 || sqxpos>=myXLength) return null;
		if(sqypos<0 || sqypos>=myYLength) return null;
		return myGrid[sqxpos][sqypos];
	}
	public String toString()
	{
		String str = new String();
		str="";
		
		str += "Map: " + myName + "\n";
			for (int lev = 0; lev < myLevels; lev++)
			{
				str+="level#";
				str+=lev;
				str+="\n";
				for (int y = 0; y < myYLength; y++)
				{
					for (int x = 0; x < myXLength; x++)
					{
						if(myGrid[x][y].get(lev)==null){
							str+=" ";
						} else {
						str += (myGrid[x][y]).get(lev).getLevel();
						}
					}
					str += "\n";
				}
				str += "\n";
			}
		return str;
	}
}
